﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI.Control {
    public partial class PageList : UserControl {

        public PageList() {
            InitializeComponent();
            labelInfo.Text = string.Empty;

            m_tbs.Add(textBox1);
            m_tbs.Add(textBox2);
            m_tbs.Add(textBox3);
            m_tbs.Add(textBox4);
            m_tbs.Add(textBox5);
            m_tbs.Add(textBox6);
            m_tbs.Add(textBox7);
            m_tbs.Add(textBox8);
            m_tbs.Add(textBox9);
            m_tbs.Add(textBox10);
            m_tbs.Add(textBox11);
            m_tbs.Add(textBox12);
            m_tbs.Add(textBox13);
            m_tbs.Add(textBox14);
        }

        private const int COUNT_PER_PAGE = 3000;
        private int m_currentPage = 0;
        private int m_pageCount = 0;

        private List<long> m_dataSource;
        public List<long> DataSource {
            get {
                return m_dataSource;
            }
            set {
                m_dataSource = value;
                ClearTextData();
                if (value == null) {
                    return;
                }

                m_pageCount = m_dataSource.Count / COUNT_PER_PAGE;
                if (m_dataSource.Count % COUNT_PER_PAGE != 0)
                    m_pageCount++;

                labelInfo.Text = string.Format("共 {0} 页", m_pageCount);
                lb.Items.Clear();
                if (m_pageCount > 0)
                    GotoPage(1);
            }
        }

        private void GotoPage(int page) {
            if (page < 1) {
                page = 1;
            }
            if (page > m_pageCount) {
                page = m_pageCount;
            }
            if (m_dataSource == null) {
                lb.Items.Clear();
                return;
            }
            m_currentPage = page;
            lb.BeginUpdate();
            lb.Items.Clear();
            int bound = Math.Min(page * COUNT_PER_PAGE, m_dataSource.Count);
            for (int i = (page - 1) * COUNT_PER_PAGE; i < bound; i++) {
                lb.Items.Add(FTHelper.LongToStr(m_dataSource[i]));
            }
            lb.Focus();
            lb.EndUpdate();
            if (lb.Items.Count != 0) {
                lb.SelectedIndex = 0;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e) {
            GotoPage(--m_currentPage);
        }

        private void toolStripButton3_Click(object sender, EventArgs e) {
            m_currentPage = 1;
            GotoPage(m_currentPage);
        }

        private void toolStripButton2_Click(object sender, EventArgs e) {
            GotoPage(++m_currentPage);
        }

        private void toolStripButton4_Click(object sender, EventArgs e) {
            m_currentPage = m_pageCount;
            GotoPage(m_currentPage);
        }

        private List<TextBox> m_tbs = new List<TextBox>();

        private void lb_SelectedIndexChanged(object sender, EventArgs e) {
            ClearTextData();

            var text = lb.Text;
            if (string.IsNullOrEmpty(text)) {
                return;
            }

            for (int i = 0; i < text.Length; i++) {
                m_tbs[i].Text = text[i].ToString();
            }
        }

        private void ClearTextData() {
            for (int i = 0; i < 14; i++) {
                m_tbs[i].Text = string.Empty;
            }
        }
    }
}
